﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using FZ.Common;
using MySql.Data.MySqlClient;
using TongJi.Model;
using System.Data;
using MySql.Data;

namespace TongJi.Dal
{
    public class TongJiDal
    {
        public static readonly string TJConnString = ConfigurationManager.ConnectionStrings["TJConn"].ConnectionString;

        public bool InsertRequestLog(RequestLogModel model)
        {
            var status = false;
            const string sqlText = @"insert tbrequestlog(iSiteId,vUserSign,vUserAgent,vIP,vUserName,vRequestDomain,vRequestUrl,vRequestPath,vReferrerDomain,vReferrerUrl,vReferrerPath,vAgentType,vSystem,vPlatform,vProxy,vBrowserName,vBrowserVersion,vBrowserDocMode,iAvailWidth,iAvailHeight,iBrowserWidth,iBrowserHeight,bSupportCookie,dtVisitTime,vPcode,vBrowserAgentName,vBrowserAgentVersion,vUserRole) 
values(?iSiteId,?vUserSign,?vUserAgent,?vIP,?vUserName,?vRequestDomain,?vRequestUrl,?vRequestPath,?vReferrerDomain,?vReferrerUrl,?vReferrerPath,?vAgentType,?vSystem,?vPlatform,?vProxy,?vBrowserName,?vBrowserVersion,?vBrowserDocMode,?iAvailWidth,?iAvailHeight,?iBrowserWidth,?iBrowserHeight,?bSupportCookie,?dtVisitTime,?vPcode,?vBrowserAgentName,?vBrowserAgentVersion,?vUserRole)
";
            using (var conn = new MySqlConnection(TJConnString))
            using (var cmd = new MySqlCommand(sqlText, conn))
            {
                cmd.Parameters.AddWithValue("?iSiteId", model.SiteId);
                cmd.Parameters.AddWithValue("?vUserSign", model.UserSign);
                cmd.Parameters.AddWithValue("?vUserAgent", model.UserAgent);
                cmd.Parameters.AddWithValue("?vIP", model.Ip);
                cmd.Parameters.AddWithValue("?vUserName", model.UserName);
                cmd.Parameters.AddWithValue("?vRequestDomain", model.RequestDomain);
                cmd.Parameters.AddWithValue("?vRequestUrl", model.RequestUrl);
                cmd.Parameters.AddWithValue("?vRequestPath", model.RequestPath);
                cmd.Parameters.AddWithValue("?vReferrerDomain", model.ReferrerDomain);
                cmd.Parameters.AddWithValue("?vReferrerUrl", model.ReferrerUrl);
                cmd.Parameters.AddWithValue("?vReferrerPath", model.ReferrerPath);
                cmd.Parameters.AddWithValue("?vAgentType", model.AgentType);
                cmd.Parameters.AddWithValue("?vSystem", model.System);
                cmd.Parameters.AddWithValue("?vPlatform", model.Platform);
                cmd.Parameters.AddWithValue("?vProxy", model.Proxy);
                cmd.Parameters.AddWithValue("?vBrowserName", model.BrowserName);
                cmd.Parameters.AddWithValue("?vBrowserVersion", model.BrowserVersion);
                cmd.Parameters.AddWithValue("?vBrowserDocMode", model.BrowserDocMode);
                cmd.Parameters.AddWithValue("?iAvailWidth", model.AvailWidth);
                cmd.Parameters.AddWithValue("?iAvailHeight", model.AvailHeight);
                cmd.Parameters.AddWithValue("?iBrowserWidth", model.BrowserWidth);
                cmd.Parameters.AddWithValue("?iBrowserHeight", model.BrowserHeight);
                cmd.Parameters.AddWithValue("?bSupportCookie", model.SupportCookie);
                cmd.Parameters.AddWithValue("?dtVisitTime", model.VisitTime);
                cmd.Parameters.AddWithValue("?vPcode", model.Pcode);
                cmd.Parameters.AddWithValue("?vBrowserAgentName", model.BrowserAgentName);
                cmd.Parameters.AddWithValue("?vBrowserAgentVersion", model.BrowserAgentVersion);
                cmd.Parameters.AddWithValue("?vUserRole", model.UserRole);

                conn.Open();
                status = cmd.ExecuteNonQuery() > 0;
                conn.Close();
            }
            return status;
        }

        public DataTable GetSiteTable()
        {
            var tb = new DataTable();
            const string sql = "SELECT ID,vSiteName SiteName,vSiteDomain SiteDomain,iStatus 'Status',vRemark Remark from tbSite";
            //using (var conn = new MySqlConnection(TJConnString))
            using (var sqlAdapter = new MySqlDataAdapter(sql, TJConnString))
            {
                sqlAdapter.Fill(tb);
            }
            return tb;
        }

        /// <summary>
        /// 返回插入的Site Id
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddSite(SiteModel model)
        {
            const string sqlText = @"insert tbSite(vSiteName,vSiteDomain,iStatus,vRemark) 
values(?vSiteName,?vSiteDomain,?iStatus,?vRemark);select @@IDENTITY;";
            using (var conn = new MySqlConnection(TJConnString))
            using (var cmd = new MySqlCommand(sqlText, conn))
            {
                cmd.Parameters.AddWithValue("?vSiteName", model.SiteName);
                cmd.Parameters.AddWithValue("?vSiteDomain", model.SiteDomain);
                cmd.Parameters.AddWithValue("?iStatus", model.Status);
                cmd.Parameters.AddWithValue("?vRemark", model.Remark);
                conn.Open();
                var i = cmd.ExecuteScalar().ToInt();
                conn.Close();
                return i;
            }
        }
    }
}
